<?php 
	//Hope custom functions
	Class Hope_Custom_functions{
		
		public function __construct(){
			
			add_action('wp_ajax_load_more', array($this,'load_more'));
			
			add_action('wp_ajax_nopriv_load_more', array($this,'load_more'));
		}
		public function get_the_excerpt_here($post_id)
		{
			global $wpdb;
			$query = "SELECT post_excerpt,post_content FROM $wpdb->posts WHERE ID = $post_id LIMIT 1";
			$result = $wpdb->get_results($query, ARRAY_A);
			if($result[0]['post_excerpt'])
				return $result[0]['post_excerpt'];
			else
				return $result[0]['post_content'];
		}
		
		static function the_excerpt_max_charlength($charlength,$post = '') {
			if($post){
				$excerpt = self :: get_the_excerpt_here($post->ID);}
			else
				$excerpt = get_the_excerpt();
			$charlength++;
			
			if(strlen($excerpt)>$charlength) {
			   $subex = substr($excerpt,0,$charlength-5);
			   $exwords = explode(" ",$subex);
			   $excut = -(strlen($exwords[count($exwords)-1]));
			   if($excut<0) {
					$result =  substr($subex,0,$excut);
			   } else {
					$result = $subex;
			   }
		   } else {
			   $result =  $excerpt;
		   }
			echo $result;
		}
		static	function print_thumbnail($post, $width, $height) {
			$thumbnail_id = get_post_thumbnail_id( $post->ID );
			$thumbnail_src = wp_get_attachment_image_src( $thumbnail_id, 'full');
			$output = get_bloginfo('template_directory').'/timthumb.php?src='.$thumbnail_src[0].'&amp;h='.$height.'&amp;w='.$width.'&amp;q=90&amp;zc=1';
		
			echo "<img src='".$output."' alt='".get_the_title($post->ID)."' title='".get_the_title($post->ID)."' />";	
		}
		
		static	function print_thumbnail_vimeo($post,$url, $width, $height) {
			$output = get_bloginfo('template_directory').'/timthumb.php?src='.$url.'&amp;h='.$height.'&amp;w='.$width.'&amp;q=90&amp;zc=1';
		
			echo "<img src='".$output."' alt='".get_the_title($post->ID)."' title='".get_the_title($post->ID)."' />";	
		}
	
		static function show_home_slider(){
			
			$main_slider = new wp_query(array('post_type'=>'post', 'meta_key'=>'slider_manager_hopeunlimited', 'meta_value'=>1));
			//update_option('thumbnail_crop', 1);
			//add_image_size('mainslidersize', 580, 260, false);
			?>
			<ul id="main-slider">
				<?php
					if($main_slider->have_posts()) : while($main_slider->have_posts()) : $main_slider->the_post();global $post;
				?>
					<li>
						<a href="<?php the_permalink() ; ?>"><?php self :: print_thumbnail($post,580,260);?></a>
						<div class="content">
							<h3 class="post-tite"><a href="<?php the_permalink();?>"><?php the_title() ; ?></a></h3>
							<div class="except"><?php Hope_Custom_functions :: the_excerpt_max_charlength(90,$post) ; ?>...</div>
						</div>
						
					</li>
				<?php
				endwhile;
				endif;
				wp_reset_query();
				?>
			</ul>
			<script type="text/javascript">
				// DOM Ready
				jQuery(function(){
					jQuery('#main-slider').anythingSlider({
						hashTags : false,
						buildNavigation : true,
						buildStartStop : false,
						buildArrows     : false,     
						autoPlay : false						
					});
					
				});
			</script>
			<?php			
			
		}
		static function show_best_of_blog(){
			$best_blog = new wp_query(array('post_type'=>'best_of_blog'));
			?>
			<div class="best-blog-containner">
				<h3 class="best-of-blog-title">BEST OF BLOGS FROM AROUND THE WORLD</h3>	
				<ul id="best-of-blog-slider">
					<?php
						$count=0;
						if($best_blog->have_posts()) : while ($best_blog->have_posts()) : $best_blog->the_post();global $post;$count++;
					?>
					<?php if($count==1 || $count%4==1) echo "<li>" ; ?>
					<?php
							$width = 0;
							$height = 0;
							if($count%4==1){$width = 181;$height = 120;};
							if($count%4==3){$width = 181;$height = 183;};
							if($count%4==2){$width = 191;$height = 183;};
							if($count%4==0){$width = 191;$height = 120;};
							
							$class="width-height-".$width."-".$height;
					?>			
						<div class="<?php echo $class;?> best-blog-slider-item <?php if($count%4==1) echo " left-top" ; if($count%4==3) echo " bottom-left" ; if($count%4==2) echo " top-right" ; if($count%4==0) echo " bottom-right" ; ?>">
							<a class="thumbnails" href="<?php the_permalink() ; ?>">
								<?php
								 self :: print_thumbnail($post,$width,$height);
								?>							
							</a>
							<a class="post-title" href="<?php the_permalink() ; ?>"><?php the_title() ; ?></a>
						</div>
					<?php if($count%4==0 || $count==$best_blog->found_posts) echo "</li>" ; ?>
					<?php
						endwhile;
						else : echo "Sorry.There are no posts to display";
						endif;
						wp_reset_query();
					?>
				</ul>
				<span class="current-bar-best-blog"></span>
				<div class='backgorund-test'></div>
			</div>
			<script type="text/javascript">
				// DOM Ready
				jQuery(function(){
					var total = jQuery('#best-of-blog-slider li').length;
					jQuery('#best-of-blog-slider').anythingSlider({
						hashTags : false,
						buildNavigation : false,
						buildStartStop : false,
						autoPlay : true	,
						forwardText : 'next' ,
						backText : 'prev',
						onInitialized : function(e, slider) {
							//updateNav(slider.currentPage-1);
							jQuery('.current-bar-best-blog').text(slider.currentPage + ' of ' + total);
						},
						// Callback before slide animates
						onSlideBegin : function(e, slider) {
							//updateNav(slider.targetPage-1);
							jQuery('.current-bar-best-blog').text(slider.targetPage + ' of ' + total);
						}
					});
					
				});
			</script>
			<?php
			
		}
		
		static function show_world_view(){
			
			$world_view = new wp_query(array('post_type' => 'world_view'));
			?>
			<li class="world-view-containner">
				<h3 class="world-view-title">WORLD VIEW</h3>	
				<ul id="world-view-slider">
					<?php
						$count=0;
						if($world_view->have_posts()) : while ($world_view->have_posts()) : $world_view->the_post();global $post;$count++;
					?>
					<?php if($count%4==1) echo "<li>" ; ?>
						<div class="world-view-slider-item">
							<a class="thumbnails" href="<?php the_permalink() ; ?>">
								<?php self :: print_thumbnail($post,79,53);?>
							</a>
							<div class="info">
								<a class="post-title" href="<?php the_permalink() ; ?>"><?php echo substr(get_the_title($post->ID),0,30)."..." ; ?></a>
								<p class="source">Find by : <a href="<?php echo get_post_meta($post->ID, 'description_world_view', true);?>"><?php echo get_post_meta($post->ID, 'description_world_view', true) ; ?></a></p>
							</div>
						</div>
					<?php if($count%4==0 || $count==$world_view->found_posts) echo "</li>" ; ?>
					<?php
						endwhile;
						else : echo "Sorry.There are no posts to display";
						endif;
						wp_reset_query();
					?>
				</ul>
				<span class="current-bar-world-view"></span>
			</li>
			<script type="text/javascript">
				
				// DOM Ready
				jQuery(function(){
					jQuery('#world-view-slider li').each(function(){
						jQuery(this).children('div:first').addClass('first');
						jQuery(this).children('div:last').addClass('last');
					});
					var total = jQuery('#world-view-slider li').length;
					jQuery('#world-view-slider').anythingSlider({
						width  : 247,
						height : 318, 
						hashTags : false,
						buildNavigation : false,
						buildStartStop : false,
						autoPlay : false	,
						forwardText : 'next' ,
						backText : 'prev',
						onInitialized : function(e, slider) {
							//updateNav(slider.currentPage-1);
							jQuery('.current-bar-world-view').text(slider.currentPage + ' of ' + total);
						},
						// Callback before slide animates
						onSlideBegin : function(e, slider) {
							//updateNav(slider.targetPage-1);
							jQuery('.current-bar-world-view').text(slider.targetPage + ' of ' + total);
						}
					});
					
				});
			</script>
			<?php
			wp_reset_query();
		}
		static function show_humor_me(){
			
			$humor_me = new wp_query(array('post_type' => 'humor_me'));
			?>
			<li class="humor-me-containner">
				<h3 class="humor_me-title">HUMOR ME</h3>	
				<ul id="humor_me-slider">
					<?php
						$count=0;
						if($humor_me->have_posts()) : while ($humor_me->have_posts()) : $humor_me->the_post();global $post;$count++;
					?>
					<li>
						<div class="humor_me-slider-item">		
								<?php
									$width = 475;
									$height = 290;							
									 self :: print_thumbnail($post,$width,$height);
								?>
						</div>
					</li>
					<?php
						endwhile;
						else : echo "Sorry.There are no posts to display";
						endif;
						wp_reset_query();
					?>
				</ul>
				<span class="current-bar-humorme"></span>
			</li>
			<script type="text/javascript">
				// DOM Ready
				jQuery(function(){
					var total = jQuery('#humor_me-slider li').length;
					jQuery('#humor_me-slider').anythingSlider({
						width  : 247,
						height : 318, 
						hashTags : false,
						buildNavigation : false,
						buildStartStop : false,
						autoPlay : false	,
						forwardText : 'next' ,
						backText : 'prev',
						onInitialized : function(e, slider) {
							//updateNav(slider.currentPage-1);
							jQuery('.current-bar-humorme').text(slider.currentPage + ' of ' + total);
						},
						// Callback before slide animates
						onSlideBegin : function(e, slider) {
							//updateNav(slider.targetPage-1);
							jQuery('.current-bar-humorme').text(slider.targetPage + ' of ' + total);
						}
					});
					
				});
			</script>
			<?php	
			
		}
		
		static function show_contest(){
			
			$contest = new wp_query(array('post_type' => 'contest'));
			?>
			<div class="contest-containner">				
				<ul id="contest-slider">
					<?php
						$count=0;
						if($contest->have_posts()) : while ($contest->have_posts()) : $contest->the_post();global $post;$count++;
					?>
					<li>
						<div class="contest-slider-item">		
								<?php
									$width = 220;
									$height = 216;							
									 self :: print_thumbnail($post,$width,$height);
								?>
						</div>
						<div class="background_contest_item">
							<h3 class="contest_title"><a href="<?php the_permalink();?>"><?php the_title() ; ?></a></h3>
							<div class="user_info">
								<?php ?>
							</div>
						</div>
					</li>
					<?php
						endwhile;
						else : echo "Sorry.There are no posts to display";
						endif;
						wp_reset_query();
					?>
				</ul>
				<span class="current-bar-humorme"></span>
			</div>
			<script type="text/javascript">
				// DOM Ready
				jQuery(function(){
					var total = jQuery('#humor_me-slider li').length;
					jQuery('#humor_me-slider').anythingSlider({
						width  : 247,
						height : 318, 
						hashTags : false,
						buildNavigation : false,
						buildStartStop : false,
						autoPlay : false	,
						forwardText : 'next' ,
						backText : 'prev',
						onInitialized : function(e, slider) {
							//updateNav(slider.currentPage-1);
							jQuery('.current-bar-humorme').text(slider.currentPage + ' of ' + total);
						},
						// Callback before slide animates
						onSlideBegin : function(e, slider) {
							//updateNav(slider.targetPage-1);
							jQuery('.current-bar-humorme').text(slider.targetPage + ' of ' + total);
						}
					});
					
				});
			</script>
			<?php	
			
		}
		
		protected function wp_parse_youtube_link($youtube_link){
			preg_match('%(?:youtube\.com/(?:user/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $youtube_link, $match);
			if(count($match) >= 2)
				return $match[1];
		    else
			   return '';
		}	
		protected function wp_parse_vimeo_link($video_url){

			if (preg_match('~^http://(?:www\.)?vimeo\.com/(?:clip:)?(\d+)~', $video_url, $match)) 
			{
				return $match[1];
			}
			else
			{
				return substr($video_url,10,strlen($video_url));
			}
		}
		protected function wp_parse_thumbnail_vimeo($vimeo_id)
		{
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, "http://vimeo.com/api/v2/video/$vimeo_id.php");
				curl_setopt($ch, CURLOPT_HEADER, 0);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($ch, CURLOPT_TIMEOUT, 10);
				$output = unserialize(curl_exec($ch));
				$output = $output[0];
				curl_close($ch);
				return $output['thumbnail_large'];
		}	
		static function get_thumbnail_video_src($video_url){
			if(strstr($video_url,'youtube.com') || strstr($video_url,'youtu.be')){
				return 'http://img.youtube.com/vi/'.self ::wp_parse_youtube_link($video_url).'/0.jpg';
			}
			else if(strstr($video_url,'vimeo.com')){
				return self :: wp_parse_thumbnail_vimeo(self :: wp_parse_vimeo_link($video_url));
			}

		}
		
		static function show_video(){
			
			$video = new wp_query(array('post_type' => 'video'));
			?>
			<li class="video-containner">
				<h3 class="video-title">VIDEO</h3>	
				<ul id="video-slider">
					<?php
						$count=0;
						if($video->have_posts()) : while ($video->have_posts()) : $video->the_post();global $post;$count++;
						$url = get_post_meta($post->ID,'video_link',true);
						$url = self :: get_thumbnail_video_src($url);
					?>
					<li>
						<div class="video-slider-item">
							<a class="thumbnails" href="<?php the_permalink() ; ?>">
								<?php self :: print_thumbnail_vimeo($post,$url,227,175);?>
							</a>
							<div class="info">
								<a class="post-title" href="<?php the_permalink() ; ?>"><?php echo substr(get_the_title($post->ID),0,20)."..." ; ?></a>
								<p class="name-user">by <?php the_author();?></p>
								<p class="shortcontent"><?php echo self :: the_excerpt_max_charlength(90, $post) ; ?></p>
							</div>
						</div>
					</li>
					<?php
						endwhile;
						else : echo "Sorry.There are no posts to display";
						endif;
						wp_reset_query();
					?>
				</ul>
				<span class="current-bar-video"></span>
			</li>
			<script type="text/javascript">
				
				// DOM Ready
				jQuery(function(){
					var total = jQuery('#video-slider li').length;
					jQuery('#video-slider').anythingSlider({
						hashTags : false,
						buildNavigation : false,
						buildStartStop : false,
						autoPlay : false	,
						forwardText : 'next' ,
						backText : 'prev',
						onInitialized : function(e, slider) {
							//updateNav(slider.currentPage-1);
							jQuery('.current-bar-video').text(slider.currentPage + ' of ' + total);
						},
						// Callback before slide animates
						onSlideBegin : function(e, slider) {
							//updateNav(slider.targetPage-1);
							jQuery('.current-bar-video').text(slider.targetPage + ' of ' + total);
						}
					});
					
				});
			</script>
			<?php
			wp_reset_query();
		}
		static function ppbv_display() {
			global $wpdb; // call global for use in function
			$ppbv_tablename = $wpdb->prefix.'popular_by_views';					
				echo "<div id='popular'>";
					echo "<ul id='popular_by_views_list' class='tabs-list'>"; // create an ordered list
						$popular = $wpdb->get_results(" 	SELECT *
															FROM `".$ppbv_tablename."`
															ORDER BY `wp_popular_by_views`.`views` DESC 
															LIMIT 0 , 5 ",ARRAY_N);
						$count=0;$class=null;
						foreach($popular as $post){ $count++;// loop through the returned array of popular posts
							if( $count==1 ) $class = 'first';
							else $class = null;
							$ID = $post[1]; // store the data in a variable to save a few characters and keep the code cleaner						
							$post_url = get_permalink($ID); // get the URL of the current post in the loop
							$title = substr(get_the_title($ID), 0, 30)."..."; // get the title of the current post in the loop
							echo "<li class='".$class."'><span class='numly'>".$count."</span><a href='{$post_url}'>{$title}</a></li>"; // echo out the information in a list-item
						} // end the loop
					echo "</ul>"; // close the ordered list
				echo "</div>";
		}
		static function show_mostdiscussed(){
			global $wpdb; // call global for use in function
			$ppbv_tablename = $wpdb->prefix.'posts';			
				echo "<div id='mostdiscussed'>";
					echo "<ul id='mostdiscussed_list' class='tabs-list'>"; // create an ordered list
						$popular = $wpdb->get_results("SELECT * FROM {$ppbv_tablename} ORDER BY comment_count DESC LIMIT 0,5",ARRAY_N);
						$count=0;$class=null;
						foreach($popular as $post){ $count++;// loop through the returned array of popular posts
							if( $count==1 ) $class = 'first';
							else $class = null;
							$ID = $post[1]; // store the data in a variable to save a few characters and keep the code cleaner						
							$post_url = get_permalink($ID); // get the URL of the current post in the loop
							$title = substr(get_the_title($ID), 0, 30)."...";// get the title of the current post in the loop
							echo "<li class='".$class."'><span class='numly'>".$count."</span><a href='{$post_url}'>{$title}</a></li>"; // echo out the information in a list-item
						} // end the loop
					echo "</ul>"; // close the ordered list
				echo "</div>";
		}
		static function show_newest(){
			$newpost = new wp_query(array('post_type'=>'post', 'posts_per_page'=>5));
			echo "<div id='newest'>";
			echo "<ul id='newest_list tabs-list' class='tabs-list'>";$count=0;
			if($newpost->have_posts()) : while($newpost->have_posts()) : $newpost->the_post();global $post;$count++;
				?>
				<li class="<?php if( $count==1 ) echo 'first' ; ?>"><span class='numly'><?php echo $count ; ?></span><a href="<?php the_permalink() ; ?>"><?php echo substr(get_the_title($post->ID), 0, 30)."..."; ?></a></li>
				<?php
			endwhile;
			endif;
			echo "</div>";
			echo "</ul>";
			wp_reset_query();
		}
		
		static function get_time_hope(){
			
			$time = localtime(time(),true);
			if($time[2] < 12) echo "<span class='morning'>THIS MORNING</span>" ;
			
			else if((12 <= $time[2])&&($time[2] < 18)) echo "<span class='afternoon'>THIS AFTERNOON</span>" ;
			
			else echo "<span class='evening'>THIS EVENING</span>" ;
		}
		static function show_flickr(){
			$id = get_option( 'flickr_id', true );
				if($id){
					$cache_file = get_template_directory().'/cache_theme/flickr_'.$id.'.txt';
					$cachetime = 60*60;
					// Time that the cache was last filled.
					$cache_file_created = ((@file_exists($cache_file))) ? @filemtime($cache_file) : 0;
					// Show file from cache if still valid.
					if (time() - $cachetime < $cache_file_created) {
						@readfile($cache_file);	
					} else {
						try{
							$ch = curl_init();
							$url = 'http://www.flickr.com/badge_code_v2.gne?source=user&user='.$id.'&count=20&display=latest&layout=x&size=t';
							curl_setopt($ch,CURLOPT_URL,$url);
							curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
							curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
							$statuses = curl_exec($ch);
							curl_close($ch);			
							$math = preg_match_all('/<a.*?href="(.*?)".*?src="(.*?)".*?<\/a>/ism',$statuses,$match);
							ob_start();
							
									?>
							<ul class="wrap">
								<?php foreach( $match[0] as $index => $image ){?>
								<?php if( ($index+1)%9 == 1 ) echo "<li>" ;?>
									<div class="flickr_badge_image"><?php echo $image;?></div>
								<?php if( ($index+1)%9 == 0 || ($index+1) == count($match[0]) ) echo "</li>" ; ?>
								<?php }?>	
							</ul>
						<script type="text/javascript">
							jQuery(function(){
								jQuery('.wrap').anythingSlider({
									hashTags : false,
									buildNavigation : false,
									buildStartStop : false,
									autoPlay : true	
								});
							})
						</script>
						<?php 
							$file = @fopen($cache_file, 'w');
				 
							// Save the contents of output buffer to the file, and flush the buffer. 
							@fwrite($file, ob_get_contents()); 
							@fclose($file); 
							ob_end_flush();
						}catch(Excetion $e){
							$result = new StdClass();
							$result->status = array();
							return $result;
						}
					}
				}
			
		}
		public function load_more(){
			$load_more = new wp_query(array('post_type'=> 'post', 'posts_per_page'=> 8,'offset'=> 11));$count=0;
			//ob_start();
			if($load_more-> have_posts()) : while($load_more->have_posts()) : $load_more->the_post();global $post;$count++;	
			?>
			<li class="home-blog-item <?php if($count == 3) echo " home-blog-item-no-margin" ;if($count%4==1&&$count!=1) echo " no-margin-right" ?>">
						<div class="post-item">
							<div class="post-item-content">
								<?php $cats = get_the_category($post->ID); ?>
								<p class="cat-list <?php echo $cats[0]->slug ; ?>"><?php foreach($cats as $key => $cat)  {echo $cat->name; if($key!=(count($cats)-1)) echo " ," ; }?></p>
								<a class="thumbnail" href="<?php the_permalink() ; ?>"><?php
								
									the_post_thumbnail('mainsize',array('title'=>get_the_title($post->ID), 'alt'=>get_the_title($post->ID)));
									
								?></a>
								<p class="post-descript"><strong class="subject"><?php echo get_post_meta($post->ID, 'title_description_post', true);?></strong><span class="des-content"><?php echo get_post_meta($post->ID, 'content_description_post', true);?></span></p>
								<h3 class="post-title"><a href="<?php the_permalink() ; ?>"><?php echo substr(get_the_title($post->ID),0,30)."..." ; ?></a></h3>
								<div class="short-content"><?php self :: the_excerpt_max_charlength(100,$post) ; ?></div>
							</div>
						</div>
			</li>
			<?php
			endwhile;
			endif;
			//ob_end_flush();
			die();
			
			
			
		}
	}
?>